﻿#include "companyservice.h"
#include <QMessageBox>
#include <QInputDialog>
#include "user.h"

CompanyService::CompanyService(QObject *parent) : QObject(parent)
{

}

void CompanyService::newCompany(QWidget *widget, QString companyName, QString contact)
{
    if(companyName.isEmpty() || contact.isEmpty()){
        QMessageBox::warning(widget, "警告", "公司名称和联系人不能为空！");
        return;
    }

    manager = new CompanyNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccess,
            [=](QString message)
    {
        QMessageBox::information(widget, "信息", message);
        emit addCompanySuccess();
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    manager->addCompany(User::getUsername(), companyName, contact);
}

void CompanyService::updateCompany(QWidget *widget,
                                   QString companyId,
                                   QString taxRate,
                                   QString companyName,
                                   QString contact,
                                   QString tel,
                                   QString fax,
                                   QString website,
                                   QString email,
                                   QString province,
                                   QString city,
                                   QString district,
                                   QString address,
                                   QString account,
                                   QString introduction,
                                   QString ticketCompanyName,
                                   QString taxNum,
                                   QString bank,
                                   QString ticketAccount,
                                   QString ticketTel,
                                   QString ticketAddress)
{
    if(!User::getIsBoss()){
        QMessageBox::warning(widget, "警告", "您不是老板不能修改企业信息！");
        return;
    }

    double taxValue = taxRate.toDouble();

    if(companyName.isEmpty() || contact.isEmpty() || taxValue < 0){
        QMessageBox::warning(widget, "警告", "公司名称和联系人不能为空！");
        return;
    }

    if(taxRate.isEmpty() || taxValue < 0){
        QMessageBox::warning(widget, "警告", "税率必须为正数！");
        return;
    }

    bool ok;
    QString res = QInputDialog::getText(widget, "输入",
                                        "请输入操作密码", QLineEdit::Password,
                                        "", &ok);

    if(ok){
        if(res == User::getOperatePassword()){
            manager = new CompanyNetworkAccessManager();

            connect(manager, &BaseNetworkAccessManager::onSuccess,
                    [=](QString message)
            {
                QMessageBox::information(widget, "信息", message);
            });

            connect(manager, &BaseNetworkAccessManager::onFailed,
                    [=](QString message)
            {
                QMessageBox::warning(widget, "警告", message);
            });

            manager->updateCompany(
                        User::getUsername(),
                        companyId,
                        QString("%1").arg(taxValue),
                        companyName,
                        contact,
                        tel,
                        fax,
                        website,
                        email,
                        province,
                        city,
                        district,
                        address,
                        account,
                        introduction,
                        ticketCompanyName,
                        taxNum,
                        bank,
                        ticketAccount,
                        ticketTel,
                        ticketAddress);
        }else{
            QMessageBox::warning(widget, "警告", "密码错误！");
        }
    }
}

void CompanyService::queryCompany(QWidget *widget, QString companyId)
{
    if(companyId.isEmpty()){
        QMessageBox::warning(widget, "警告", "企业ID不能为空！");
        return;
    }

    manager = new CompanyNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccessWithData,
            [=](QString message, QJsonObject data)
    {
        QJsonObject companyObj = data.value("company").toObject();
        Company company;
        company.loadFromJsonObject(companyObj);
        QMessageBox::information(widget, "信息", message);
        emit queryCompanySuccess(company);
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    manager->queryCompany(User::getUsername(), companyId);
}

void CompanyService::joinCompany(QWidget *widget, QString companyId)
{
    if(companyId.isEmpty()){
        QMessageBox::warning(widget, "警告", "企业ID不能为空！");
        return;
    }

    manager = new CompanyNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccessWithData,
            [=](QString message, QJsonObject data)
    {
        QJsonObject companyObj = data.value("company").toObject();
        Company company;
        company.loadFromJsonObject(companyObj);
        company.saveToSettings();
        QMessageBox::information(widget, "信息", message);
        User::setCompanyId(company.companyId);
        emit joinCompanySuccess();
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    manager->joinCompany(User::getUsername(), companyId);
}

void CompanyService::updateCategory(QWidget *widget, Company::CategoryType categoryType, QString category)
{
    if(category.isEmpty()){
        QMessageBox::warning(widget, "警告", "类别不能为空！");
        return;
    }
    if(User::getCompanyId().isEmpty())
    {
        QMessageBox::warning(widget, "警告", "您还没有公司无法进行当前操作！");
        return;
    }

    manager = new CompanyNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    manager->updateCategory(categoryType, User::getCompanyId(), category);
}

void CompanyService::updateCategory(
        QWidget *widget,
        Company::CategoryType categoryType,
        QString oldPrimaryDirectory,
        QString oldSecondaryDirectory,
        QString newPrimaryDirectory,
        QString newSecondaryDirectory)
{
    manager = new CompanyNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccessWithData,
            [=](QString message, QJsonObject data)
    {
        QMessageBox::information(widget, "信息", message);
        QString newCategory = data.value("newCategory").toString();
        emit editOK(newCategory);
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    manager->updateCategory(
                categoryType,
                User::getUsername(),
                User::getCompanyId(),
                oldPrimaryDirectory,
                oldSecondaryDirectory,
                newPrimaryDirectory,
                newSecondaryDirectory);
}

void CompanyService::deleteCategory(
        QWidget *widget,
        Company::CategoryType categoryType,
        QString primaryDirectory,
        QString secondaryDirectory)
{
    manager = new CompanyNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccess,
            [=](QString message)
    {
        Q_UNUSED(message);
        emit deleteOK();
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    manager->deleteCategory(
                categoryType,
                User::getUsername(),
                User::getCompanyId(),
                primaryDirectory,
                secondaryDirectory);
}

void CompanyService::updateCompanyData(QWidget *widget, bool showMsg){
    manager = new CompanyNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccessWithData,
            [=](QString message, QJsonObject data)
    {
        if(showMsg){
            QMessageBox::information(widget, "信息", message);
        }
        QJsonObject companyObj = data.value("company").toObject();
        double totalStore = data.value("totalStore").toDouble();
        Company company;
        company.loadFromJsonObject(companyObj);
        company.saveToSettings();
        this->updateCompanyDataSuccess(totalStore);
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    manager->updateCompanyData(User::getUsername(), Company::getCompanyId());
}

void CompanyService::homeCount(QWidget *widget, QPushButton *button, int type, double oldValue, double countValue){
    if(countValue == 0.0){
        QMessageBox::warning(widget, "警告", "盘点金额不能为0");
        return;
    }

    manager = new CompanyNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        button->setEnabled(true);
        QMessageBox::warning(widget, "警告", message);
    });

    connect(manager, &BaseNetworkAccessManager::onSuccess,
            [=](QString message)
    {
        button->setEnabled(true);
        QMessageBox::information(widget, "信息", message);
        widget->close();
    });

    button->setEnabled(false);
    manager->homeCount(
                type,
                User::getUsername(),
                Company::getCompanyId(),
                oldValue,
                countValue);
}
